/**
 * plugin.js
 *
 * Copyright, Moxiecode Systems AB
 * Released under LGPL License.
 *
 * License: http://www.tinymce.com/license
 * Contributing: http://www.tinymce.com/contributing
 */

/*jshint unused:false */
/*global tinymce:true */

/**
 * Example plugin that adds a toolbar button and menu item.
 */
tinymce.PluginManager.add('boxcontent', function (editor, url) {

    // General settings shared between simple and advanced dialogs
    var generalFormItems = [
        { type : 'label', label : 'Content :'},
        { type: 'textbox', name: 'code', multiline: true, minWidth: 400, minHeight: 220, name: 'content' },
        {
            type: 'form',
            layout: 'grid',
            columns: 1,
            padding: 0,
            alignH: ['left', 'right'],
            defaults: {
                type: 'textbox',
                maxWidth: 80,
            },
            items: [
                    { label: 'Box Width :', name: 'boxw', value: '300' }
            ]
        }
    ];

    function onSubmitForm(e) {
        // Insert content when the window form is submitted
        var sHtml = '';
        var width = e.data.boxw;
        var content = e.data.content;
        content = content.replace(/\n|\r\n|\r/g, "<br />");
        sHtml += '<p><table width="' + width + '" align="center" style="border: 1px solid #000; background:#fffce3; border-collapse:collapse"><tbody><tr>';
        sHtml += '<td>'+ content +'</td>';
        sHtml += '</tr></table></p>';

        editor.insertContent(sHtml);
    }
    // Add a button that opens a window
    editor.addButton('boxcontent', {
        text: 'Box',
        onclick: function () {
            // Open window
            editor.windowManager.open({
                title: 'Box Content',
                body: [
                   {
                       title: 'General',
                       type: 'form',
                       items: generalFormItems
                   }
                ],
                onsubmit: function (e) {
                    onSubmitForm(e);
                }
            });
        }
    });

    editor.addMenuItem('example', {
        text: 'Box Content',
        context: 'insert',
        onclick: function () {
            // Open window
            editor.windowManager.open({
                title: 'Box Content',
                body: [
                   {
                       title: 'General',
                       type: 'form',
                       items: generalFormItems
                   }
                ],
                onsubmit: function (e) {
                    onSubmitForm(e);
                }
            });
        }
    });
});